1
|
|
|
/** global: google, r, g, b */ |
2
|
|
|
|
3
|
|
|
import { IconObject } from "./icon_object.js"; |
4
|
|
|
|
5
|
|
|
import { createClusterIcon } from "./create_cluster_icon.js"; |
6
|
|
|
import { createTextMarker } from "./create_text_marker.js"; |
7
|
|
|
|
8
|
|
|
import { createFatMarkerIcon } from "./create_fat_marker_icon.js"; |
9
|
|
|
|
10
|
|
|
import { createGroupedIcon } from "./create_grouped_icon.js"; |
11
|
|
|
|
12
|
|
|
import { createTransparentMarkerIcon } from "./create_transparent_marker_icon.js"; |
13
|
|
|
|
14
|
|
|
import { parseHex, parseHSL, parseRGB, hslToRGB, rgbToHSL } from "./parsers.js"; |
15
|
|
|
|
16
|
|
|
import { omit, serializeOptions } from "./helpers.js"; |
17
|
|
|
|
18
|
|
|
function padHex(str_in) { |
19
|
|
|
if (("" + str_in).length === 1) { |
20
|
|
|
return "0" + String(str_in); |
21
|
|
|
} else { |
|
|
|
|
22
|
|
|
return String(str_in); |
23
|
|
|
} |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
const MarkerFactory = { |
27
|
|
|
createTransparentMarkerIcon: createTransparentMarkerIcon, |
28
|
|
|
createFatMarkerIcon: createFatMarkerIcon, |
29
|
|
|
createTextMarker: createTextMarker, |
30
|
|
|
createClusterIcon: createClusterIcon, |
31
|
|
|
createGroupedIcon: createGroupedIcon, |
32
|
|
|
|
33
|
|
|
readCache: function(cacheKey, options) { |
34
|
|
|
if (options.no_cache) { |
35
|
|
|
return null; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
var cached = window.sessionStorage.getItem(cacheKey); |
39
|
|
|
if (cached === null) { |
40
|
|
|
return null; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
var cachedObj = JSON.parse(cached); |
44
|
|
|
var iconObj = new IconObject( |
45
|
|
|
cachedObj.url, |
46
|
|
|
cachedObj.fillColor, |
47
|
|
|
omit(cachedObj, function(key) { |
48
|
|
|
return ["url", "fillColor"].indexOf(key) !== -1; |
49
|
|
|
}) |
50
|
|
|
); |
51
|
|
|
return iconObj; |
52
|
|
|
}, |
53
|
|
|
|
54
|
|
|
setCache: function(cacheKey, iconObj) { |
55
|
|
|
var cached = iconObj.toJSON(); |
56
|
|
|
cached.url = iconObj.url; |
57
|
|
|
window.sessionStorage.setItem(cacheKey, JSON.stringify(cached)); |
58
|
|
|
return iconObj; |
59
|
|
|
}, |
60
|
|
|
|
61
|
|
|
generateAutoicon: function(options) { |
62
|
|
|
var generatorFN; |
63
|
|
|
|
64
|
|
|
if (!options.is_icon) { |
65
|
|
|
options.type = "textmarker"; |
66
|
|
|
generatorFN = MarkerFactory.createTextMarker; |
67
|
|
|
} else if (options.shadow || options.type === "grouped") { |
68
|
|
|
options.type = "grouped"; |
69
|
|
|
generatorFN = MarkerFactory.createGroupedIcon; |
70
|
|
|
} else if (options.transparent_background) { |
71
|
|
|
options.type = "transparent"; |
72
|
|
|
generatorFN = MarkerFactory.createTransparentMarkerIcon; |
73
|
|
|
} else { |
74
|
|
|
generatorFN = MarkerFactory.createFatMarkerIcon; |
75
|
|
|
options.type = "fatmarker"; |
76
|
|
|
} |
77
|
|
|
var cacheKey = serializeOptions(options); |
78
|
|
|
var iconObj = MarkerFactory.readCache(cacheKey, options); |
79
|
|
|
if (iconObj === null) { |
80
|
|
|
iconObj = generatorFN(options); |
81
|
|
|
} |
82
|
|
|
if (options.no_cache) { |
83
|
|
|
return iconObj; |
84
|
|
|
} |
85
|
|
|
return MarkerFactory.setCache(cacheKey, iconObj); |
86
|
|
|
}, |
87
|
|
|
/** |
88
|
|
|
* Receives a color string rgb(a), hsl(a) or hex, returns its components |
89
|
|
|
* in rgba and hsla, with optional transparency |
90
|
|
|
* plus a darkened version (default is half of each RGB component) and a |
91
|
|
|
* |
92
|
|
|
* @param {string} somecolor - A color string in rgb(a), hsl(a) or hex format |
93
|
|
|
* @param {Number} opacity - Opacity to apply to the color. Optional, default 1 |
94
|
|
|
* @param {Number} darkenfactor - How much darker should the resulting color be. Optional, default 1 |
95
|
|
|
* |
96
|
|
|
* @return {Object} input color parsed and modified as requested |
97
|
|
|
*/ |
98
|
|
|
parseColorString: function(somecolor, opacity, darkenfactor) { |
99
|
|
|
let parsedcolor = { |
100
|
|
|
original: somecolor |
101
|
|
|
}, |
102
|
|
|
hsl, |
103
|
|
|
rgb; |
104
|
|
|
|
105
|
|
|
darkenfactor = darkenfactor || 1; |
106
|
|
|
opacity = isNaN(parseFloat(opacity, 10)) ? 1 : parseFloat(opacity, 10); |
107
|
|
|
|
108
|
|
|
if (somecolor.indexOf("hsl") !== -1) { |
109
|
|
|
hsl = parseHSL(somecolor, opacity); |
110
|
|
|
rgb = hslToRGB(hsl.h, hsl.s, hsl.l, hsl.a, darkenfactor); |
111
|
|
|
} else if (somecolor.indexOf("rgb") !== -1) { |
112
|
|
|
rgb = parseRGB(somecolor, opacity, darkenfactor); |
113
|
|
|
} else { |
114
|
|
|
rgb = parseHex(somecolor, opacity, darkenfactor); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
hsl = rgbToHSL(rgb.r, rgb.g, rgb.b, rgb.a); |
118
|
|
|
|
119
|
|
|
parsedcolor.hsl = { |
120
|
|
|
h: hsl.h, |
121
|
|
|
s: hsl.s, |
122
|
|
|
l: hsl.l, |
123
|
|
|
a: hsl.a |
124
|
|
|
}; |
125
|
|
|
parsedcolor.rgb = { |
126
|
|
|
r: rgb.r, |
127
|
|
|
g: rgb.g, |
128
|
|
|
b: rgb.b, |
129
|
|
|
a: rgb.a |
130
|
|
|
}; |
131
|
|
|
|
132
|
|
|
parsedcolor.fillColor = rgb.fillColor; |
133
|
|
|
parsedcolor.rgba = rgb.fillColor; |
134
|
|
|
parsedcolor.hsla = hsl.fillColor; |
135
|
|
|
parsedcolor.strokeColor = rgb.strokeColor; |
136
|
|
|
parsedcolor.hex = [ |
137
|
|
|
"#", |
138
|
|
|
padHex(rgb.r.toString(16)), |
139
|
|
|
padHex(rgb.g.toString(16)), |
140
|
|
|
padHex(rgb.b.toString(16)), |
141
|
|
|
rgb.a === 0 ? "00" : "" |
142
|
|
|
].join(""); |
143
|
|
|
return parsedcolor; |
144
|
|
|
}, |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Generates an google maps marker (or an image as dataurl from the given options) |
148
|
|
|
* |
149
|
|
|
* @param {Object} options The options |
150
|
|
|
* @return {Object} { description_of_the_return_value } |
151
|
|
|
*/ |
152
|
|
|
autoIcon: function(options) { |
153
|
|
|
if (typeof options !== "object") { |
154
|
|
|
console.warn("autoIcon expects an object as its only parameter"); |
155
|
|
|
return null; |
156
|
|
|
} |
157
|
|
|
// unless explicitly set to false, the icon doesn't have a marker-like wrapper |
158
|
|
|
options.transparent_background = |
159
|
|
|
options.transparent_background !== false; |
160
|
|
|
|
161
|
|
|
options.label = String(options.label || "A"); |
162
|
|
|
options.color = options.color || "#FF0000"; |
163
|
|
|
|
164
|
|
|
if ( |
165
|
|
|
options.label.length === 4 || |
166
|
|
|
options.label.substring(0, 2) === "0x" |
167
|
|
|
) { |
168
|
|
|
options.font = options.font || "fontello"; |
169
|
|
|
options.label = (options.label || "e836").slice(-4); |
170
|
|
|
options.unicodelabel = String.fromCharCode("0x" + options.label); |
171
|
|
|
options.scale = options.scale || 1; |
172
|
|
|
options.is_icon = true; |
173
|
|
|
|
174
|
|
|
return MarkerFactory.generateAutoicon(options); |
175
|
|
|
} else if (options.shadow) { |
176
|
|
|
console.log("createClusterIcon", JSON.stringify(options)); |
|
|
|
|
177
|
|
|
|
178
|
|
|
return MarkerFactory.createClusterIcon(options); |
179
|
|
|
} else { |
180
|
|
|
options.scale = options.scale || 0.75; |
181
|
|
|
options.label = String(options.label || "A"); |
182
|
|
|
options.fontsize = options.fontsize || 11; |
183
|
|
|
options.font = options.font || "Arial"; |
184
|
|
|
// This is text I should print literally |
185
|
|
|
|
186
|
|
|
return MarkerFactory.generateAutoicon(options); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
}; |
190
|
|
|
|
191
|
|
|
export { MarkerFactory }; |
192
|
|
|
|